package top.ezzz.search.model;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

import java.util.function.Consumer;

/**
 * @author noear
 * @since 1.0.14
 */
public class EsHighlight {
    private final JSONObject oNode;

    public EsHighlight(JSONObject oNode) {
        this.oNode = oNode;
    }

    public EsHighlight addField(String field, Consumer<EsHighlightField> consumer) {
        if (oNode.get("fields") == null) {
            oNode.set("fields", JSONUtil.createObj());
        }
        JSONObject oNode1 = oNode.getJSONObject("fields");
        if (oNode1.get(field) == null) {
            oNode1.set(field, JSONUtil.createObj());
        }
        JSONObject oNode2 = oNode1.getJSONObject(field);
        consumer.accept(new EsHighlightField(oNode2));
        return this;
    }
}
